﻿//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Documents
//  File name:	LookupFieldWithPickerPropertyBag.cs
//  Developer:	Solomatov Igor
//  Created:	16.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System;
using System.Collections.Generic;
using Smart.Common;

// ReSharper disable CheckNamespace

namespace Smart.Documents.Fields.LookupFieldWithPicker
// ReSharper restore CheckNamespace
{
	public class LookupFieldWithPickerPropertyBag
	{
		public Guid ListId { get; set; }
		public Guid FieldId { get; set; }
		public int MaxSearchResults { get; set; }
		public int EntityEditorRows { get; set; }
		public Guid WebId { get; set; }
		public int ItemID { get; set; }
		public string FieldName { get; set; }
		public string SearchQuery { get; set; }

		private string _searchFields = "";

		public List<string> SearchFields
		{
			get { return new List<string>(_searchFields.Split(',')); }
			set
			{
				var str = "";

				foreach (var strField in value)
				{
					if (str.Length > 0)
						str += ",";
					str += strField;
				}

				_searchFields = str;
			}
		}

		public LookupFieldWithPickerPropertyBag()
		{
		}

		public LookupFieldWithPickerPropertyBag(string value)
		{
			var tokens = value.Split(';');

			ListId = new Guid(tokens[0]);
			FieldId = new Guid(tokens[1]);
			_searchFields = tokens[2];
			MaxSearchResults = int.Parse(tokens[3]);
			EntityEditorRows = int.Parse(tokens[4]);
			WebId = new Guid(tokens[5]);
			ItemID = int.Parse(tokens[6]);
			FieldName = tokens[7];
			SearchQuery = tokens[8].FromBase64();

			if (MaxSearchResults < 0)
				MaxSearchResults = 0;
		}

		public LookupFieldWithPickerPropertyBag(Guid webId, Guid listId, Guid fieldId, List<string> searchFields, int maxSearchResults, int entityEditorRows, int itemID, string fieldName,
		                                        string searchQuery)
		{
			ListId = listId;
			FieldId = fieldId;
			SearchFields = searchFields;
			MaxSearchResults = maxSearchResults;
			EntityEditorRows = entityEditorRows;
			WebId = webId;
			ItemID = itemID;
			FieldName = fieldName;
			SearchQuery = searchQuery;

			if (MaxSearchResults < 0)
				MaxSearchResults = 0;
		}

		public override string ToString()
		{
			if (MaxSearchResults < 0)
				MaxSearchResults = 0;

			return
				ListId.ToString() + ";" + FieldId.ToString() + ";" + _searchFields + ";" +
				MaxSearchResults + ";" + EntityEditorRows + ";" + WebId.ToString() + ";" +
				ItemID.ToString() + ";" + FieldName + ";" + SearchQuery.ToBase64();
		}
	}
}